package com.tiancheng.trade.order.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Data
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "credit-merchant-info-config")
public class MerchantInfoConfig {

    private List<MerchantConfigInfo> list;

    public String getCreditSystemMerchantCode(String pId) {
        if (!CollectionUtils.isEmpty(this.list)) {
            List<MerchantConfigInfo> collect = this.list.stream().filter(m -> {
                boolean exit = false;
                String[] mIds = m.getPlatformId().split(",");
                for (String mId : mIds) {
                    if (StringUtils.isNotEmpty(mId) && mId.equals(pId)) {
                        exit = true;
                        break;
                    }
                }
                return exit;
            }).collect(Collectors.toList());
            return CollectionUtils.isEmpty(collect) ? pId : collect.get(0).getCreditSystemId();
        }
        return pId;
    }
}
